#!/bin/sh -e
#
### BEGIN INIT INFO
# Provides:          sks
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
### END INIT INFO
#
# Start/stops the sks daemons.
#
#

# Reads config file
[ -r /etc/default/sks ] && . /etc/default/sks

DAEMON=/usr/sbin/sks
SKSDBPID=/var/run/sks/sksdb.pid
SKSRECONPID=/var/run/sks/sksrecon.pid
SKSLOGDIR=/var/log/sks
SKSBASEDIR=/var/lib/sks
SKSDBDIR=$SKSBASEDIR/DB
SKSPTREEDIR=$SKSBASEDIR/www
SKSDIFFDIR=/var/spool/sks
DB_RECOVER=/usr/bin/db_recover

# See if the daemon is there

test -e $DAEMON || exit 0

if [ ! -x $DAEMON ]; then
    echo "ERROR: $DAEMON is not executable"
    exit 1
fi

wait_for_deaddaemon () {
	pidfile=$1
	pid=`cat $pidfile 2>/dev/null` || true
	sleep 1
	if test -n "$pid"
	then
		if kill -0 $pid 2>/dev/null
		then
			echo -n "."
			cnt=0
			while kill -0 $pid 2>/dev/null
			do
				cnt=`expr $cnt + 1`
				if [ $cnt -gt 30 ]
				then
					echo " FAILED."
					return 1
				fi
				sleep 2
				echo -n "."
			done
			rm -f $pidfile
		else
			rm -f $pidfile
		fi
	fi
	return 0
}


case "$1" in
    start)

	if [ "$initstart" != "yes" ]; then
	    echo " Not starting sks (as configured in /etc/default/sks)"
	    exit 0
	fi

	mkdir -p `dirname "$SKSDBPID"`
	chown debian-sks `dirname "$SKSDBPID"`
	mkdir -p "$SKSLOGDIR"
	chown -R debian-sks:adm "$SKSLOGDIR"
	chmod -R g+rX "$SKSLOGDIR"
	chmod    g+s  "$SKSLOGDIR"
	chown -R debian-sks "$SKSBASEDIR" "$SKSDIFFDIR"


	echo -n "PTree database recovery."
	cd $SKSPTREEDIR
	$DB_RECOVER -h $SKSPTREEDIR &
	wait
	echo -n "."
	$DB_RECOVER -c -h $SKSPTREEDIR &
	wait
	echo " done."
	echo -n "DB database recovery."
	cd $SKSDBDIR
	$DB_RECOVER -h $SKSDBDIR &
	wait
	echo -n "."
	$DB_RECOVER -c -h $SKSDBDIR &
	wait
	echo " done."

	echo -n "Starting sks daemons:"

	echo -n " sksdb.."

	start-stop-daemon --start --quiet --oknodo \
			  --chuid debian-sks:debian-sks \
			  --background \
			  --make-pidfile \
			  --pidfile $SKSDBPID \
			  --exec $DAEMON -- db &

	echo -n " sksrecon.."

	start-stop-daemon --start --quiet --oknodo \
			  --chuid debian-sks:debian-sks \
			  --background \
			  --make-pidfile \
			  --pidfile $SKSRECONPID \
			  --exec $DAEMON -- recon &

	echo " done."

    ;;
    stop)
	echo -n "Stopping sks daemons:"

	echo -n " sksrecon.."
	start-stop-daemon --stop --quiet --oknodo --pidfile $SKSRECONPID
	wait_for_deaddaemon $SKSRECONPID || exit 1

	echo -n " sksdb.."
	start-stop-daemon --stop --quiet --oknodo --pidfile $SKSDBPID
	wait_for_deaddaemon $SKSDBPID || exit 1

	echo " done."

    ;;
    reload)
	# Hupping at least reopens logfiles.
	# membership and mailsync are regularily read anyway
	# not sure about the rest
	#    -- weasel
	echo -n "Reloading sks daemons:"
	echo -n " sksrecon.."
	start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile $SKSRECONPID

	echo -n " sksdb.."
	start-stop-daemon --stop --signal 1 --quiet --oknodo --pidfile $SKSDBPID

	echo " done."
    ;;
    restart|force-reload)
	$0 stop
	sleep 1
	$0 start
    ;;
    *)
	echo "Usage: $0 {start|stop|reload|restart|force-reload}"
	exit 1
    ;;
esac

exit 0
